<?php
// +----------------------------------------------------------------------
// | HkCms 插件后台管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace addon\message\controller;

use app\addons\util\Adminaddonbase;

class Admin extends Adminaddonbase
{
    /**
     * 插件后台列表
     * @return mixed|void
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //搜索行为标识
        $keyword = trim(input('keyword'));

        if (!empty($keyword)) {
            $where['name|content'] = array('like', '%' . $keyword . '%');
            $this->assign('keyword', $keyword);
        } else {
            $where = null;
        }

        $query['m'] = request()->module();
        $query['c'] = request()->controller();
        $query['a'] = request()->action();
        $query['isadmin'] = 1;

        $data = db('message')->where($where)->order('create_at')->paginate('1',false,['query'=>$query]);
        $page = $data->render();
        $this->assign('data', $data->toArray()['data']);
        $this->assign('page', $page);
        return $this->display();
    }

    /**
     * 插件留言删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $id = input('id');
        if (empty($id) || !is_numeric($id)) {
            $this->error('非法ID');
        }

        $id = db('message')->where(['id'=>$id])->delete();
        if ($id) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
